package com.song.study.springclouddemo.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;

import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.sl.extractor.SlideShowExtractor;
import org.apache.poi.sl.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFAutoShape;
import org.apache.poi.xslf.usermodel.XSLFComment;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTextBox;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

public class PPT {


    //直接抽取幻灯片的全部内容
    public static String readDoc1(InputStream is) throws IOException {
        PowerPointExtractor extractor=new PowerPointExtractor(is);

        return extractor.getText();
    }

    public static void main(String[] args) throws IOException {

        String filePath = "PPPT.pptx";
        File file = new File(filePath);
        FileInputStream inputStream = new FileInputStream(file);


        XMLSlideShow slideShow = new XMLSlideShow(inputStream);
        SlideShowExtractor  slideShowExtractor = new SlideShowExtractor(slideShow);
        System.out.println(slideShowExtractor.getText());

        List<XSLFPictureData> pictureData = slideShow.getPictureData();
        System.out.println("pic size: " + pictureData.size());

        // 所有幻灯片
        List<XSLFSlide> slides = slideShow.getSlides();

        slides.forEach(item -> {
            // 幻灯片中所有区域形状
            List<XSLFShape> shapes = item.getShapes();
            for (XSLFShape shape : shapes) {
                if (shape instanceof XSLFTextBox){
                    XSLFTextBox xslfTable = (XSLFTextBox) shape;
                    System.out.println(xslfTable.getText());
                }else if (shape instanceof XSLFAutoShape){
                    XSLFAutoShape autoShape = (XSLFAutoShape) shape;
                    System.out.println(autoShape.getText());
                }else if (shape instanceof XSLFPictureShape) {
                    XSLFPictureShape pictureShape = (XSLFPictureShape) shape;
                    URI pictureLink = pictureShape.getPictureLink();
                    System.out.println(pictureLink.getPath());
                }
            }
        });


    }
}
